package com.tanhua.autoconfig.template;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.tanhua.autoconfig.properties.OssProperties;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

public class OssTemplate {

    private OssProperties properties;

    public OssTemplate(OssProperties properties){
        this.properties =properties;
    }

    public String upload(String filename, InputStream is){
         filename =new SimpleDateFormat("yyyy/MM/dd").format(new Date())
                +"/"+ UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
        String endpoint = properties.getEndpoint();
        String accessKeyId =properties.getAccessKey();
        String accessKeySecret = properties.getSecret();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId,accessKeySecret);

        ossClient.putObject(properties.getBucketName(), filename,is);
        ossClient.shutdown();
        String url =properties.getUrl()+"/"+filename;
        return  url;
    }
}
